// JavaScript Document
function searchLi(id,obj){
	return false;
}
function ubaci(){
	
	var sl = document.getElementById("searchList");
	var mb = document.getElementById("moveBuffer");

	var mbLis = mb.getElementsByTagName("li");
	var idjevi=Array();
	if(mbLis.length){
		for(var i=0; i<mbLis.length; i++){
			idjevi.push(mbLis[i].value);
		}
	}
	
	var lis = sl.getElementsByTagName("li");
	
	var imavec=false;
	for(var i=0; i<lis.length; i++){
		if(lis[i].className=="move"){
			if(in_array(lis[i].value,idjevi) === false){
				lis[i].className = "";
				mb.appendChild(lis[i]);
				i--;
			}else{
				imavec=true;
			}
		}
	}
	if(imavec){
		setStatus("<span class=\"error\">Oznacene delatnosti su vec dodate.</span>");
	}else{
		setStatus("");
	}
	updateStatus('moveBuffer','searchInfo1');
	updateStatus('searchList','searchInfo');
}
function izbaci(){
	var sl = document.getElementById("searchList");
	var mb = document.getElementById("moveBuffer");
	
	var slLis = sl.getElementsByTagName("li");
	var idjevi=Array();
	if(slLis.length){
		for(var i=0; i<slLis.length; i++){
			idjevi.push(slLis[i].value);
		}
	}
	
	var lis = mb.getElementsByTagName("li");
	var imavec=false;
	for(var i=0; i<lis.length; i++){
		if(lis[i].className=="move"){
			var s = in_array(lis[i].value,idjevi);
			if(s === false){
				sl.insertBefore(lis[i],sl.childNodes[0]);
			}else{
				mb.removeChild(lis[i]);
				imavec=true;
			}
			i--;
		}
	}
	
	sl.scrollTop = "200px";
	setStatus("");
	
	updateStatus('moveBuffer','searchInfo1');
	updateStatus('searchList','searchInfo');
}
function in_array(needle, haystack){
	if(haystack.length){
		for(var i=0; i<haystack.length; i++){
			if(needle == haystack[i]){
				return i;
			}
		}
		return false;
	}else{
		return false;
	}
}
function setStatus(str){
	document.getElementById("status").innerHTML = str;
}
function updateStatus(buff,sinfo){
	var mb = document.getElementById(buff);
	var st = document.getElementById(sinfo);
	var lis = mb.getElementsByTagName("li");
	
	if(lis.length){
		sel=0;
		for(var i=0; i<lis.length; i++){
			if(lis[i].className=='move'){
				sel++;
			}
		}
	}else{
		sel=0;
	}
	st.innerHTML = sel+" / "+lis.length;
}
function select(li){
	li.className=(li.className=='move')?'':'move';
	switch(li.parentNode.id){
		case "searchList":
			updateStatus('searchList','searchInfo');
			break;
		case "moveBuffer":
			updateStatus('moveBuffer','searchInfo1');
			break;
	}
}
function selectAll(id,state){
	var cn = state==1?"move":"";
	var elem = document.getElementById(id);
	var lis = elem.getElementsByTagName("li");
	for(var i=0; i<lis.length; i++){
		lis[i].className = cn;
	}
	switch(id){
		case "searchList":
			updateStatus('searchList','searchInfo');
			break;
		case "moveBuffer":
			updateStatus('moveBuffer','searchInfo1');
			break;
	}
}
function updateSearchList(response){
	var sl = document.getElementById('searchList');
	var si = document.getElementById('searchInfo');
	sl.innerHTML = response;
	
	setStatus("");
	updateStatus('searchList','searchInfo');
//	var length = sl.getElementsByTagName("li").length;
//	si.innerHTML = length;
}
function Execute(){
	var maxAskList=30;
	
	var mb = document.getElementById("moveBuffer");
	var lis = mb.getElementsByTagName("li");
	var udel = document.getElementById("uDelatnost");
	out = "";
	if(!lis.length){
		alert("Niste odabrali nijednu delatnost.");
		return;
	}
	var move = Array();
	var send = "";
	var ask = "";
	for(var i=0; i<lis.length; i++){
		move[lis[i].value]=lis[i].innerHTML;
		send += ","+lis[i].value;
		if(i<maxAskList){
			ask  += "\n"+lis[i].value+".\t"+lis[i].innerHTML;
		}else{
			if(i==maxAskList){
				ask += "\n...";
			}
		}
	}
	send = "&from="+send.substr(1)+"&to="+udel.value
	
	if(confirm("Potvrdite prebacivanje firmi iz sledecih delatnosti:\n"+ask+"\n\n\nu delatnost:\n"+udel.value+". "+udel.options[udel.selectedIndex].innerHTML)){
		LoadRSDoc("?a=exec"+send,"alert");
	}
}